package com.liuhaozzu.zookeeper.zookeeper_app.data;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;

//异步更新节点内容
public class UpdataDataAsync implements Watcher{

	private static CountDownLatch connectedSemaphore=new CountDownLatch(1);
	private static ZooKeeper zk;
	public static void main(String[] args) throws Exception {
		String path="/liuhaozzu";
		zk=new ZooKeeper("hadoop1:2181",5000, new UpdataDataAsync());
		connectedSemaphore.await();
		zk.create(path, "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		
		zk.setData(path, "456".getBytes(), -1, new IStatCallback(),null);
		Thread.sleep(Integer.MAX_VALUE);
	}
	@Override
	public void process(WatchedEvent event) {
		if (KeeperState.SyncConnected==event.getState()) {
			if (EventType.None ==event.getType() && null ==event.getPath()) {
				connectedSemaphore.countDown();
			} 
		}
	}
}

class IStatCallback implements AsyncCallback.StatCallback{

	@Override
	public void processResult(int rc, String path, Object ctx, Stat stat) {
		if (rc==0) {
			System.out.println("success");
		}
	}
	
}